<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>

<script type="text/javascript">
$(document).ready(function(){
    $("#categoryLevel0").change(function() {
        var selectedVal = $("#categoryLevel0").val();
        $('#<?php echo CHtml::activeId($model, $fieldName ); ?>').val(selectedVal);
        $('#Category_level').val(1);
        $('#categoryLevel1').css('display', selectedVal=='' ? 'none' : '');
        if (selectedVal != '') {
            getCategoriesByParentAndLevel(0);
        }
    });
    
    $("#categoryLevel1").change(function() {
        var selectedVal = $("#categoryLevel1").val();
        $('#<?php echo CHtml::activeId($model, $fieldName ); ?>').val(selectedVal == '' ? $('#categoryLevel0').val() : selectedVal);
        $('#Category_level').val(selectedVal == '' ? 1 : 2);
        
        $('#categoryLevel2').css('display', selectedVal=='' ? 'none' : '');
        if (selectedVal != '') {
            getCategoriesByParentAndLevel(1);
        }
    });
    /*
    $("#categoryLevel2").change(function() {
        var selectedVal = $("#categoryLevel2").val();
        $('#<?php echo CHtml::activeId($model, $fieldName ); ?>').val(selectedVal == '' ? $('#categoryLevel1').val() : selectedVal);
        $('#Category_level').val(selectedVal == '' ? 1 : 2);
    });
    */
});

function getCategoriesByParentAndLevel(level) {
    <?php echo CHtml::ajax(array(
                // the controller/function to call
                'url'=>CController::createUrl('category/getCategoriesByParentAndLevel'), 
 
                'data'=>array('parent_id'=>'js:$(\'#categoryLevel\'+level).val()',
                               'YII_CSRF_TOKEN'=>Yii::app()->request->csrfToken,),
                'type'=>'post',
                'dataType'=>'json',
                'success'=>"function(data)
                {
                    var cats = data.categories;
                    var options = '<option value=\"\">fără categorie</option>';
                    for (var i = 0; i < cats.length; i++) {
                        options += '<option value=\"' + cats[i].id + '\">' + cats[i].name + '</option>';
                    }
                    $('select#categoryLevel'+(level+1)).html(options);
                }", ));
    ?>;
    return false;
}
</script>

<div class="row">
        <?php echo $form->labelEx($model, $fieldName); ?>
        <?php echo CHtml::dropDownList('categoryLevel0', $fieldName, Category::categoriesByLevel(0), array('empty'=>'fără categorie')); ?>
        <?php if ($fieldName == 'category_id') echo CHtml::dropDownList('categoryLevel1', $fieldName, array(), array('empty'=>'fără categorie', 'style'=>'display:none;')); ?>
        <?php //if ($fieldName == 'category_id') echo CHtml::dropDownList('categoryLevel2', $fieldName, array(), array('empty'=>'fără categorie', 'style'=>'display:none;')); ?>
        <?php echo $form->hiddenField($model, $fieldName) ?>
    
        <?php if ($fieldName == 'parent_id') echo $form->hiddenField($model, 'level'); ?>
        <?php echo $form->error($model, $fieldName); ?>
    
</div>